import QtQuick 2.8
import QtQuick.Window 2.2
import QtQuick.Controls 2.1
import QtWeChat 1.0
import "./controls"

Page {
    id: root
    width: 280
    height: 400

    property WeChatLogin login

    background: Item {}

    header: TitleBar {
        title: ApplicationWindow.window.title
        isWindow: false

        TitleButton {
            source: "qrc:/images/btn_login_set.png"
        }
    }

    Connections {
        target: login
        onStatusChanged: {
            switch(login.status) {
            case 1:
                loginLoader.sourceComponent = loginCode;
                break;
            case 2:
                loginLoader.sourceComponent = loginAvatar;
                break;
            default:
                break;
            }
        }
    }

    Component {
        id: loginCode
        Image {
            anchors.centerIn: parent
            source: login.loginCode
            sourceSize.width: 188
            sourceSize.height: 188
        }
    }

    Component {
        id: loginAvatar
        Item {
            anchors.fill: parent
            Image {
                anchors.horizontalCenter: parent.horizontalCenter
                source: login.loginAvatar
            }

            Button {
                id: switchLogin
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.bottom: parent.bottom
                background: Rectangle {
                    implicitWidth: 100
                    implicitHeight: 40
                    opacity: enabled ? 1 : 0.3
                    color: switchLogin.down ? "#17a81a" : "#21be2b"
                    radius: 2
                }
                contentItem: Text {
                    text: switchLogin.text
                    font: switchLogin.font
                    color: "white"
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideRight
                }
                text: qsTr("切换账号")
            }
        }
    }

    Loader {
        id: loginLoader
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: parent.top
        anchors.topMargin: 50
        width: 188
        height: 188
    }

    footer: Item {
        width: parent.width
        height: 60
        Label {
            anchors.horizontalCenter: parent.horizontalCenter
            text: qsTr("请使用微信扫一扫以登录")
        }
    }

    Component.onCompleted: {
        ApplicationWindow.window.visible = false;
        ApplicationWindow.window.width = root.width;
        ApplicationWindow.window.height = root.height;
        ApplicationWindow.window.x = (Screen.desktopAvailableWidth - root.width) / 2;
        ApplicationWindow.window.y = (Screen.desktopAvailableHeight - root.height) / 2;
        ApplicationWindow.window.visible = true;
    }
}
